package StudentCode;

import java.awt.Color;
import GridTools.MyGrid;

public class FlagMaker {

	/*
	 * this method prints the flag of the country Poland
	 * it only displays Poland's flag if grid size is even
	 * otherwise Error flag is displayed
	 * grid is divided in half
	 * top half is left white(the original color of grid) and bottom half
	 * is colored red
	 */
	
	public static void Poland(MyGrid grid, int countryCode) {
		if (grid.getHt() % 2 == 0) {
			for (int row = grid.getHt() - 1; row >= (grid.getHt() / 2); row--) {
				for (int col = 0; col <= grid.getWd() - 1; col++) {

					grid.setColor(row, col, Color.RED);
				}
			}

		} else {
			ErrorFlag(grid, countryCode);
		}
	}

	/*
	 * this method prints the flag of the country Ukraine
	 * it only displays Ukraine's flag if grid size is even
	 * otherwise Error flag is displayed
	 * then rows of blue and yellow are added
	 */
	
	public static void Ukraine(MyGrid grid, int countryCode) {
		if (grid.getHt() % 2 == 0) {
			for (int row = 0; row <= (grid.getHt() / 2) - 1; row++) {
				for (int col = 0; col <= grid.getWd() - 1; col++) {
					grid.setColor(row, col, Color.BLUE);
				}
			}
			for (int row = grid.getHt() - 1; row >= (grid.getHt() / 2); row--) {
				for (int col = 0; col <= grid.getWd() - 1; col++) {
					grid.setColor(row, col, Color.YELLOW);
				}
			}
		}else{
			ErrorFlag(grid, countryCode);
		}
	}
	
	/*
	 * this method prints the flag of the country Czech Republic
	 * it only displays Czech Republic's flag if the grid size is even
	 * otherwise Error flag is displayed
	 * blue colored triangle on the left of height "size" and increasing 
	 * width of "2" to "(size/2)+1" is coded first and then followed by another
	 * blue colored triangle on the left of height "size" and decreasing width
	 * of "(size/2)+1"to "2"
	 */

	public static void TheCzechRepublic(MyGrid grid, int countryCode) {
		if(grid.getHt() % 2 ==0){
			Poland(grid, countryCode);
			for (int row = 0; row < (grid.getHt() / 2); row++) {
				for (int col = 0; col <= row + 1; col++) {
					grid.setColor(row, col, Color.BLUE);
				}
			}
			int colCounter = 1;
			for (int row = grid.getHt()-1; row >= grid.getHt()/2; row--) {
				for (int col = 0; col <= colCounter; col++) {
					grid.setColor(row, col, Color.BLUE);
				}
				colCounter = colCounter + 1;
			}
		}else{
			ErrorFlag(grid, countryCode);
		}
	}

	/*
	 * this method prints the flag of the country Benin
	 * it only displays Benin's flag if grid size is a multiple of 6
	 * otherwise it displays Error flag
	 * first the method Poland(which colors the grid top half white, bottom half 
	 * red) is called to color the background for the Benin flag
	 * then the top half is colored yellow
	 * after that green columns are added to the left of the flag which are 1/3
	 * of the width of grid
	 */
	
	public static void Benin(MyGrid grid, int countryCode) {
		if (grid.getHt() % 6 == 0) {
			Poland(grid, countryCode);
			for (int row = 0; row < (grid.getHt() / 2); row++) {
				for (int col = 0; col <= grid.getWd() - 1; col++) {
					grid.setColor(row, col, Color.YELLOW);
				}
			}
			int colWidth = ((grid.getWd() / 3));
			for (int row = 0; row < grid.getHt(); row++) {
				for (int col = 0; col <= colWidth - 1; col++) {
					grid.setColor(row, col, Color.GREEN);
				}
			}
		}else{
			ErrorFlag(grid, countryCode);
		}
	}

	/*
	 * this method prints the flag of the country Rwanda
	 * it only displays Rwanda's flag when the grid size is multiple of 4
	 * otherwise Error flag is displayed
	 * first the method Ukraine is called for the background of Rwanda flag 
	 * then green rows are added to the grid that color over the background color
	 * that was previously yellow
	 */
	
	public static void Rwanda(MyGrid grid, int countryCode) {
		if (grid.getHt() % 4 == 0) {
			Ukraine(grid, countryCode);

			for (int row = (3 * grid.getHt() / 4); row <= grid
					.getHt() - 1; row++) {
				for (int col = 0; col <= grid.getWd() - 1; col++) {
					grid.setColor(row, col, Color.GREEN);
				}
			}

		}else{
			ErrorFlag(grid, countryCode);
		}
	}

	/* 
	 * this method prints the flag of the country Bahamas
	 * it only displays Bahama's flag if the grid size is even
	 * otherwise the Error flag is displayed
	 * first the whole grid is colored blue and then yellow rows are added in 
	 * the middle of the grid
	 * the black triangle on the left of height "size" and increasing width of
	 * "2" to "(size/2)+1" and black triangle on the left of height "size" 
	 * and decreasing width of "(size/2)+1"to "2" are coded
	 */
	
	public static void Bahamas(MyGrid grid, int countryCode) {
		if (grid.getHt() % 2 == 0) {
			for (int row = 0; row <= grid.getHt() - 1; row++) {
				for (int col = 0; col <= grid.getWd() - 1; col++) {
					grid.setColor(row, col, Color.BLUE);
				}
			}

			int middle1 = (grid.getHt() / 2) - 1;
			int middle2 = (grid.getHt() / 2);

			for (int row = middle1; row <= middle2; row++) {
				for (int col = 0; col < grid.getWd(); col++) {
					grid.setColor(row, col, Color.YELLOW);
				}
			}

			for (int row = 0; row < (grid.getHt() / 2); row++) {
				for (int col = 0; col <= row + 1; col++) {
					grid.setColor(row, col, Color.BLACK);
				}
			}
			int colCounter = 1;
			for (int row = grid.getHt()-1; row >= grid.getHt()/2; row--) {
				for (int col = 0; col <= colCounter; col++) {
					grid.setColor(row, col, Color.BLACK);
				}
				colCounter = colCounter + 1;
			}
		}else{
			ErrorFlag(grid, countryCode);
		}
	}

	/*
	 * this method prints the flag of the country Afghanistan
	 * it only displays Afghanistan's flag if the grid size is a multiple of 3
	 * otherwise an Error flag is displayed
	 * the flag of Afghanistan has 3 sets of different colored columns
	 * each set of colored column has a nested 'for loop'
	 */
	
	public static void Afghanistan(MyGrid grid, int countryCode) {
		if (grid.getHt() % 3 == 0) {
			int coloredRow = grid.getHt();
			int coloredCol = grid.getWd() / 3;// each set of colored column is
			                                  // 1/3 the whole grid
			
			// red colored set of columns                               
			for (int row = 0; row <= grid.getHt() - 1; row++) {
				for (int col = 0; col <= grid.getWd() - 1; col++) {
					grid.setColor(row, col, Color.RED);
				}
			}
			//black colored set of columns
			for (int row = 0; row <= coloredRow - 1; row++) {
				for (int colBlack = 0; colBlack <= coloredCol - 1; colBlack++) {
					grid.setColor(row, colBlack, Color.BLACK);
				}
			}
			//green colored set of columns
			for (int rowGreen = 0; rowGreen <= coloredRow-1; rowGreen++) {
				for (int colGreen = grid.getWd() - 1; colGreen >= (2 * grid
						.getWd() / 3); colGreen--) {
					grid.setColor(rowGreen, colGreen, Color.GREEN); 
					   
				}
			}
		}else{
			ErrorFlag(grid, countryCode);
		}
	}

	/*
	 * this method prints the flag of the country Jersey
	 * it only displays Jersey's flag if the grid size entered is odd
	 * otherwise Error flag is displayed
	 * the grid is initially colored all white.
	 * the following code makes a red cross across the whole of the grid and 
	 * colors two columns red in one row in the process
	 */
	
	public static void Jersey(MyGrid grid, int countryCode) {
		if (grid.getHt() % 2 == 0) {
			ErrorFlag(grid, countryCode);
		} else {

			for (int row = 0; row < grid.getHt(); row++) {
				for (int col = row * 2; col <= row * 2 + 1; col++) {
					grid.setColor(row, col, Color.RED);
				}
			}
			int colCounter = 0;
			for (int row = grid.getHt() - 1; row >= 0; row--) {
				for (int col = colCounter * 2; col <= colCounter * 2 + 1; col++) {
					grid.setColor(row, col, Color.RED);
				}
				colCounter = colCounter + 1;

			}
		}

	}

	/*
	 * this method prints the flag of the country Scotland
	 * it only displays Scotland's flag if the grid size entered is odd
	 * otherwise Error flag is displayed
	 * first the whole grid is colored blue and then the white is added
	 * (which colors over the blue in certain places)
	 * to make a cross across the whole grid
	 */
	
	public static void Scotland(MyGrid grid, int countryCode) {
		if(grid.getHt() % 2 != 0){
			for (int row = 0; row < grid.getHt(); row++) {
				for (int col = 0; col < grid.getWd(); col++) {
					grid.setColor(row, col, Color.BLUE);
				}
			}

			for (int row = 0; row < grid.getHt(); row++) {
				for (int col = row * 2; col <= row * 2 + 1; col++) {
					grid.setColor(row, col, Color.WHITE);
				}
			}
			int colCounter = 0;
			for (int row = grid.getHt() - 1; row >= 0; row--) {
				for (int col = colCounter * 2; col <= colCounter * 2 + 1; col++) {
					grid.setColor(row, col, Color.WHITE);
				}
				colCounter = colCounter + 1;
			}
		}else{
			ErrorFlag(grid, countryCode);
		}
	}
	
	/*
	 * this method prints an ErrorFlag when arguments passed do not satisfy the 
	 * parameters for a certain Flag 
	 */
	
	public static void ErrorFlag(MyGrid grid, int countryCode) {
		for (int row = 0; row < grid.getHt(); row = row + grid.getHt()-1) {
			for (int col = 0; col < grid.getWd(); col = col + grid.getWd()-1) {
				grid.setColor(row, col, Color.RED);

			}
		}
	}

	/*
	 * The skeleton given here just calls an example flag to be put into the
	 * grid passed as the first parameter. (note: In its current form it is
	 * ignoring the second parameter.)
	 * 
	 * You need to modify this method so that the correct flag (the one that
	 * corresponds to the country code passed as the second parameter) is
	 * colored correctly in the grid passed in as the first parameter. Note: you
	 * can assume the grid is already created an waiting for you to put the
	 * correct colors into the correct squares. You can also assume that the
	 * method that calls the one you are writing here will take care of actually
	 * displaying the flag - you do not need to do anything about that.
	 */
	
	public static void drawFlag(MyGrid grid, int countryCode) {
		if(!(countryCode >=1 && countryCode <=9)){
			ErrorFlag(grid, countryCode);
		}
		if (countryCode == 1) {
			Poland(grid, countryCode);
		}
		if (countryCode == 2) {
			Ukraine(grid, countryCode);
		}
		if (countryCode == 3) {
			TheCzechRepublic(grid, countryCode);
		}
		if (countryCode == 4) {
			Benin(grid, countryCode);
		}
		if (countryCode == 5) {
			Rwanda(grid, countryCode);
		}
		if (countryCode == 6) {
			Bahamas(grid, countryCode);
		}
		if (countryCode == 7) {
			Afghanistan(grid, countryCode);
		}
		if (countryCode == 8) {
			Jersey(grid, countryCode);
		}
		if (countryCode == 9) {
			Scotland(grid, countryCode);
		} 


	}
}
